package  
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.media.SoundMixer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.ui.Keyboard;
	import style.TextStyle;
	/**
	 * ...
	 * @author jun
	 */
	public class MMTextInput extends MMComponent 
	{
		private const MIN_WIDTH:Number = 160;
		private const MIN_HEIGHT:Number = 22;
		
		private var _textField:TextField;
		private var _maxChars:uint;
		private var _text:String="";
		private var _width:Number;
		private var _height:Number;
		private var _needUpdate:Boolean;
		private var _border:Boolean;
		private var _displayAsPassword:Boolean;
		private var _textStyle:TextStyle;
		
		private var _textColor:uint;
		
		public var onChange:Function;
		public var onEnter:Function;
		
		public function MMTextInput(maxChars:uint=60) 
		{
			super();
			_maxChars = maxChars;
			_needUpdate = true;
			mouseChildren = true;
			_width = MIN_WIDTH;
			_height = MIN_HEIGHT;
		}
		
		override public function onInit():void
		{
			super.onInit();
			_textField = new TextField();
			_textField.width = _width;
			_textField.height = _height;
			_textField.autoSize = TextFieldAutoSize.NONE;
			_textField.type = TextFieldType.INPUT;
			_textField.borderColor = 0x000000;
			_textField.maxChars = _maxChars;
			addChild(_textField);
			
			_textField.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler, false, 0, true);
			_textField.addEventListener(Event.CHANGE,onChangeHandler,false,0,true)
		}
		
		private function onChangeHandler(e:Event):void 
		{
			onChange && onChange(e);
			e.stopPropagation();
		}
		
		private function onKeyUpHandler(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				onEnter && onEnter();
			}
			e.stopPropagation();
		}
		
		override public function onHide():void
		{
			super.onHide();
		}
		
		override public function onShow():void
		{
			super.onShow();
			update();
		}
		
		override public function updateData():void
		{
			super.updateData();
			if (_needUpdate)
			{
				if (_textField.width != _width)
					_textField.width = _width;
				_textField.height = _height;
					_textField.height = _height;
				
				if (	_textField.text != _text)
				{
					_textField.text = _text;
					if (_textStyle)
					{
						if(_textStyle.textFormat)
							_textField.setTextFormat(_textStyle.textFormat);
						if (_textStyle.filter)
							_textField.filters = _textField.filters;
					}
					else
					{
						if (_textField.filters)
							_textField.filters = null;
					}
					
					onChange && onChange();
				}
				
				if (_border != _textField.border)
				{
					_textField.border = _border;
				}
				if (_textField.displayAsPassword != _displayAsPassword)
					_textField.displayAsPassword = _displayAsPassword;
				
				if (_enable)
				{
					
				}
				if (_textField.textColor != _textColor)
					_textField.textColor = _textColor;
				
			}
		}
		
		override public function get width():Number { return _width; }
		
		override public function set width(value:Number):void 
		{
			if (_width != value && value > MIN_HEIGHT)
			{
				_width = value;
				_needUpdate = true;
				update();
				
			}
			
		}
		
		override public function get height():Number { return _height; }
		
		override public function set height(value:Number):void 
		{
			if (_height != value && value > MIN_WIDTH)
			{
				_height = value;
				_needUpdate = true;
				update();
			}
			
		}
		
		public function get text():String { return _text; }
		
		public function set text(value:String):void 
		{
			_text = value;
			_needUpdate = true;
			update();
		}
		
		public function get border():Boolean { return _border; }
		
		public function set border(value:Boolean):void 
		{
			if (_border != value)
			{
				_border = value;
				_needUpdate = true;
				update();
				
			}
			
		}
		
		public function get displayAsPassword():Boolean { return _displayAsPassword; }
		
		public function set displayAsPassword(value:Boolean):void 
		{
			if (_displayAsPassword != value)
			{
				_displayAsPassword = value;
				_needUpdate = true;
				update();
			}
			
		}
		
		public function get textColor():uint { return _textColor; }
		
		public function set textColor(value:uint):void 
		{
			_textColor = value;
			if (_textColor != value)
			{
				_textColor = value;
				_needUpdate = true;
				update();
			}
		}
		
		public function get textStyle():TextStyle { return _textStyle; }
		
		public function set textStyle(value:TextStyle):void 
		{
			if (_textStyle != value)
			{
				_textStyle = value;
				_needUpdate = true;
				update();
			}
		}
		
		override public function gc():void
		{
			if(_textField )_textField = null;
			if(_textStyle)_textStyle = null;
			if(onChange !=null)onChange = null;
			if(onEnter !=null)onEnter = null;
			super.gc();
		}
	}

}